{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from IPython.display import display\n",
    "\n",
    "pd.set_option('display.max_columns', 100)\n",
    "\n",
    "inf = pd.read_csv(\"/firecares/FireCares_Dept_List.csv\").dropna(axis='columns', how='all')\n",
    "#inf = pd.read_csv(\"/firecares/firecares/firestation/tests/mock/local_numbers.csv\").dropna(axis='columns', how='all')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "inf.groupby(['fdid', 'State_Code']).count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "inf[(inf['IAFF_local'] == '4416')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "inf[inf['firecares_id'].isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "inf[['fdid', 'State_Code', 'firecares_id', 'IAFF_local']].to_csv('/tmp/locals.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "groups = inf[['fdid', 'State_Code', 'firecares_id', 'IAFF_local']].groupby(['fdid', 'State_Code'])\n",
    "keys = map(lambda x: x[0], groups)\n",
    "ret = []\n",
    "\n",
    "for k in keys:\n",
    "    g = groups.get_group(name=k)\n",
    "    fc_id = g[g['firecares_id'].notnull()]['firecares_id'].values\n",
    "    if fc_id.any():\n",
    "        print 'IMPORTING: {} with local numbers: {}'.format(fc_id[0], ','.join(map(str, g['IAFF_local'].values)))\n",
    "        ret.append({'fc_id': fc_id[0], 'locals': ','.join(map(str, g['IAFF_local'].values))})\n",
    "    else:\n",
    "        rec = inf.loc[g.index[0],:]\n",
    "        print 'SKIPPING {},{}:\\n{}'.format(rec['fdid'], rec['State_Code'], rec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ret"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
